#include "headfile/login.h"
#include "headfile/iconhelper.h"
#include "ui_login.h"
#include "headfile/myhelper.h"
#include "headfile/restcontrol.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QMouseEvent>

login::login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    this->initStyle();
}

login::~login()
{
    delete ui;
}

void login::initStyle()
{
    //this->setWindowState(Qt::WindowMaximized);
    setWindowTitle(QString("登录"));
    setWindowIcon(QIcon(":/image/logo.png"));

    ui->loginPushButton->setDefault(true);
    //setStyle(QString("blue"));
    IconHelper::Instance()->SetIcon(ui->closeBtn, QChar(0xf00d), 18);
    //IconHelper::Instance()->SetIcon(ui->btnMenu_Max, QChar(0xf096), 10);
    IconHelper::Instance()->SetIcon(ui->minBtn, QChar(0xf068), 18);
    //IconHelper::Instance()->SetIcon(ui->btnMenu, QChar(0xf0c9), 10);
    IconHelper::Instance()->SetIcon(ui->lab_Ico, QChar(0xf015), 18);
    //设置窗体标题栏隐藏
    this->setWindowFlags(Qt::FramelessWindowHint);
    //设置窗体关闭时自动释放内存
    //this->setAttribute(Qt::WA_DeleteOnClose);

    // 隐藏文本输入框内容为密码模式
    ui->pwdLineEdit->setEchoMode(QLineEdit::Password);
    ui->passModeBtn->setFlat(true);
    ui->passModeBtn->setIcon(QIcon(":/image/hide.png"));
    btnPassMode = true;
    max = false;
    mousePressed = false;
    ui->userLineEdit->setFocus();

}

void login::setStyle(const QString &styleName)
{
    QFile file(QString(":/uiImage/%1.css").arg(styleName));
    file.open(QFile::ReadOnly);
    QString qss = QLatin1String(file.readAll());
    setStyleSheet(qss);
    setPalette(QPalette(QColor("#F0F0F0")));
}

// 登录检查
void login::on_loginPushButton_clicked()
{
    QString sUsername = ui->userLineEdit->text().trimmed();
    QString sPassword = ui->pwdLineEdit->text().trimmed();

    int status = 0;
    if(sUsername.isEmpty()){
        //QMessageBox::information(this,QString("请输入用户名"),QString("用户名不能为空"),QMessageBox::Ok);
        myHelper::ShowMessageBoxError("用户名不能为空");
        ui->userLineEdit->setFocus();
    }
    else if (sPassword.isEmpty()) {
        //QMessageBox::information(this,QString("请输入密码"),QString("密码不能为空"),QMessageBox::Ok);
        myHelper::ShowMessageBoxError("密码不能为空");
        ui->pwdLineEdit->setFocus();
    }
    else{
        // -1 登录失败, 1 用户名或密码错误 0 验证成功 2 用户名错误
        restcontrol resctl;
        status = resctl.checkUserAndPass(sUsername, sPassword);

        if(status == 0) {
            QDialog::accept();
        } else if(status == -1) {
            //QMessageBox::critical(this, QString("登录"), QString("登录失败"), QMessageBox::Ok);
            myHelper::ShowMessageBoxError("登录失败");
        }else{
            //QMessageBox::critical(this,QString("密码验证错误"),QString("用户名或密码错误"),QMessageBox::Ok);
            myHelper::ShowMessageBoxError("用户名或密码错误");
            ui->pwdLineEdit->clear();
            ui->pwdLineEdit->setFocus();
        }
    }
}

void login::on_cancelPushButton_clicked()
{
    QDialog::reject();
}

//切换密码是否可见
void login::on_passModeBtn_clicked()
{
    if(btnPassMode)
    {
        ui->pwdLineEdit->setEchoMode(QLineEdit::Normal);
        ui->passModeBtn->setIcon(QIcon(":/image/show.png"));
        btnPassMode = false;
    }
    else {
        ui->pwdLineEdit->setEchoMode(QLineEdit::Password);
        ui->passModeBtn->setIcon(QIcon(":/image/hide.png"));
        btnPassMode = true;
    }
}

// 为窗口拖动重写的鼠标移动事件
//void mousePressEvent(QMouseEvent *e);
//void mouseMoveEvent(QMouseEvent *e);
//void mouseReleaseEvent(QMouseEvent *e);
void login::mouseMoveEvent(QMouseEvent *e)
{
    if (mousePressed && (e->buttons() && Qt::LeftButton) && !max) {
        this->move(e->globalPos() - mousePoint);
        e->accept();
    }
}

void login::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        mousePressed = true;
        mousePoint = e->globalPos() - this->pos();
        e->accept();
    }
}

void login::mouseReleaseEvent(QMouseEvent *)
{
    mousePressed = false;
}

void login::on_minBtn_clicked()
{
    this->setWindowState(Qt::WindowMinimized);
}

void login::on_closeBtn_clicked()
{
    QDialog::reject();
}
